/*
 * Copyright (C) 2012 The Aliyunos Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.way.filemanager;

import android.app.Application;
import android.content.pm.ApplicationInfo;
import android.util.Log;


import com.way.filemanager.R;
import com.way.filemanager.navigation.MyFileManager;
import com.way.filemanager.preferences.AccessMode;
import com.way.filemanager.preferences.FileManagerSettings;
import com.way.filemanager.preferences.ObjectStringIdentifier;
import com.way.filemanager.preferences.Preferences;
import com.way.filemanager.util.AIDHelper;
import com.way.filemanager.util.CommonFunc;
import com.way.filemanager.util.MimeTypeHelper;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * A class that wraps the information of the application (constants,
 * identifiers, statics variables, ...).
 *
 * @hide
 */
public final class FileManagerApplication extends Application {

    private static final String TAG = "FileManagerApplication"; //$NON-NLS-1$

    private static boolean DEBUG = false;

    private static Map<String, Boolean> sOptionalCommandsMap;

    /**
     * A constant that contains the main process name.
     *
     * @hide
     */
    public static final String MAIN_PROCESS = "com.aliyunos.filemanager"; //$NON-NLS-1$

    // Static resources
    private static FileManagerApplication sApp;


    private static boolean sIsDebuggable = false;
    private static boolean sIsDeviceRooted = false;


    /**
     * {@inheritDoc}
     */
    @Override
    public void onCreate() {
        if (DEBUG) {
            Log.d(TAG, "FileManagerApplication.onCreate"); //$NON-NLS-1$
        }

        CommonFunc.TA_AppOncreate(this);
        MyFileManager.onCreate(this);

        init();
        //register();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onTerminate() {
        MyFileManager.onTerminate();
        if (DEBUG) {
            Log.d(TAG, "onTerminate"); //$NON-NLS-1$
        }

        super.onTerminate();
    }

    /**
     * Method that register the application context.
     */
    private void register() {
        // Register the notify broadcast receiver
    }

    /**
     * Method that initializes the application.
     */
    private void init() {
        // Save the static application reference
        sApp = this;

        // Read the system properties
        //sSystemProperties = new Properties();
        //readSystemProperties();

        // Check if the application is debuggable
        //sIsDebuggable = (0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE));

        // Check if the device is rooted
        //sIsDeviceRooted = areShellCommandsPresent();

        // Check optional commands
        //loadOptionalCommands();

        // Sets the default preferences if no value is set yet
        //Preferences.loadDefaults();

        // Read AIDs
        AIDHelper.init(getApplicationContext());

       // Force the load of mime types
        /*
        try {
            MimeTypeHelper.loadMimeTypes(getApplicationContext());
        } catch (Exception e) {
            Log.e(TAG, "Mime-types failed.", e); //$NON-NLS-1$
        }
        */
    }

    /**
     * Method that returns the singleton reference of the application.
     *
     * @return Application The application singleton reference
     * @hide
     */
    public static FileManagerApplication getInstance() {
        return sApp;
    }

    /**
     * Method that returns if the application is debuggable
     *
     * @return boolean If the application is debuggable
     */
    public static boolean isDebuggable() {
        return sIsDebuggable;
    }

    /**
     * Method that returns if the device is rooted
     *
     * @return boolean If the device is rooted
     */
    public static boolean isDeviceRooted() {
        return sIsDeviceRooted;
    }

    /**
     * Method that returns if a command is present in the system
     *
     * @param commandId
     *            The command key
     * @return boolean If the command is present
     */
    public static boolean hasOptionalCommand(String commandId) {
        if (!sOptionalCommandsMap.containsKey(commandId)) {
            return false;
        }
        return sOptionalCommandsMap.get(commandId).booleanValue();
    }

    /**
     * Method that returns the access mode of the application
     *
     * @return boolean If the access mode of the application
     */
    public static AccessMode getAccessMode() {
        if (!sIsDeviceRooted) {
            return AccessMode.SAFE;
        }
        String defaultValue = ((ObjectStringIdentifier) FileManagerSettings.SETTINGS_ACCESS_MODE
                .getDefaultValue()).getId();
        String id = FileManagerSettings.SETTINGS_ACCESS_MODE.getId();
        AccessMode mode = AccessMode.fromId(Preferences.getSharedPreferences()
                .getString(id, defaultValue));
        return mode;
    }


}
